package com.hndzyu.yd.my;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.hndzyu.yd.R;
import com.hndzyu.yd.bean.ProblemBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by YJ on 2018/10/23.
 */
public class CommonProblemAdapter extends RecyclerView.Adapter<CommonProblemAdapter.ViewHolder> {
    private Context mContext;
    private List<ProblemBean> mData = new ArrayList<>();
    private int currentItem = -1;

    public CommonProblemAdapter(List<ProblemBean> dataSet,Context context) {
        mData = dataSet;
        mContext = context;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_problem, parent, false);
        return new ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ProblemBean dataBean = mData.get(position);
        holder.mRadioButton.setTag(position);
        holder.mRadioButton.setText(Html.fromHtml("<b>Q：</b>" +dataBean.getTitle()));
        holder.tvContent.setText(Html.fromHtml("<font color=\"#FF0000\">A：</font>" + dataBean.getContent()));
        if (currentItem == position) {
            holder.tvContent.setVisibility(View.VISIBLE);
        } else {
            holder.tvContent.setVisibility(View.GONE);
        }
        holder.mRadioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int tag = (Integer) view.getTag();
                if (tag == currentItem) {
                    currentItem = -1;
                } else {
                    currentItem = tag;
                }
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView mRadioButton;
        TextView tvContent;
        public ViewHolder(View itemView) {
            super(itemView);
            tvContent = itemView.findViewById(R.id.tv_content);
            mRadioButton = itemView.findViewById(R.id.rb);
        }
    }
}
